import Cookies from "js-cookie";

function f(){
    return Cookies.get('LORC') === 'true'

}
function useThatFunction(type,value){
    const newVal = JSON.stringify(value)

    if (f() && type === 'add') {
        localStorage.setItem('TABLEDATALOCAL', newVal)
    }else if (!f() && type === 'add') {
        Cookies.set('TABLEDATACOOKIES', newVal)
    }

    if (f() && type === 'get'){
       return localStorage.getItem('TABLEDATALOCAL') ? JSON.parse(localStorage.getItem('TABLEDATALOCAL')) : []
    }else if (!f() && type === 'get'){
       return Cookies.get('TABLEDATACOOKIES') ? JSON.parse(Cookies.get('TABLEDATACOOKIES')) : []
    }
}

export function removeValueFromArray(array, value) {
    // 使用filter方法创建一个新数组，过滤掉与指定值相等的项
    return array.filter(item => item.id !== value.id);
}

export function messageTip(color,message){
    const h = this.$createElement;

    this.$notify({
        title: '提示',
        message: h('i', { style: `color: ${color}`}, `${message}成功`)
    });
}

/**
 * 设置那个本地的存储的值
 * @param activeName
 * @param value
 */
export function isCookies(activeName,value){
    useThatFunction('add',value)
}

/**
 * 获取那个本地存储的数据
 * @param activeName
 * @returns {any|*[]}
 */
export function getThatData() {
    return useThatFunction('get')
}

/**
 * 删除那个数据
 * @param activeName
 * @param row
 */
export function deleteData(activeName,row){
    if (activeName === 'localstorage') {
        const arr = JSON.parse(localStorage.getItem('TABLEDATALOCAL'))
        const newTableData = JSON.stringify(removeValueFromArray(arr,row))
        localStorage.setItem('TABLEDATALOCAL', newTableData)
    }else{
        const arr = JSON.parse(Cookies.get('TABLEDATACOOKIES'))
        const newTableData = JSON.stringify(removeValueFromArray(arr,row))
        Cookies.set('TABLEDATACOOKIES', newTableData)
    }
}
